//
//  JDOrderListReleaseCell.swift
//  DragonWallet
//
//  Created by volantPig on 2025/4/4.
//

import UIKit
import SwiftyJSON

class JDOrderListReleaseCell: UITableViewCell {

    var containerView: UIView?
    
    var releaseTypeTitleLabel: UILabel?
    var orderStateLabel: UILabel?
    var orderStateImageV: UIImageView?
    
    var topLine: UIView?
    
    var contentLabel: UILabel?
    
    var middleLine: UIView?
    /// 合计金额
    var totalPriceLabel: UILabel?
    /// 支付时间
    var payTimeLabel: UILabel?
    
    var bottomLine: UIView?
    var bottomFunctionView: UIView?
    var cancelButton: UIButton?
    var payButton: UIButton?
    
    var orderType: JDOrderType = .waitingPay
    
    var orderModel: JDOrderListModel? {
        didSet {
            self.refreshData()
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.contentView.backgroundColor = color("#F5F6F8")
        self.setupUIElements()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUIElements() {
        self.containerView = UIView()
        self.contentView.addSubview(self.containerView!)
        self.containerView?.snp.makeConstraints { make in
            make.left.equalTo(self.contentView).offset(12)
            make.right.equalTo(self.contentView).offset(-12)
            make.top.equalTo(self.contentView).offset(0)
            make.bottom.equalTo(self.contentView).offset(-10)
        }
        self.containerView?.backgroundColor = .white
        self.containerView?.layer.cornerRadius = 8
        
        self.topLine = UIView()
        self.containerView?.addSubview(self.topLine!)
        self.topLine?.snp.makeConstraints { make in
            make.leading.equalTo(self.containerView!).offset(12)
            make.trailing.equalTo(self.containerView!).offset(-12)
            make.top.equalTo(self.containerView!).offset(52)
            make.height.equalTo(1)
        }
        self.topLine?.backgroundColor = color("#F5F6F8")
        
        self.releaseTypeTitleLabel = UILabel()
        self.containerView?.addSubview(self.releaseTypeTitleLabel!)
        self.releaseTypeTitleLabel?.snp.makeConstraints { make in
            make.leading.equalTo(self.containerView!).offset(12)
            make.top.equalTo(self.containerView!)
            make.bottom.equalTo(self.topLine!.snp.top)
        }
        self.releaseTypeTitleLabel?.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 16, weight: .semibold), textColor: color("#333333"), numberOfLines: 1)
        
        self.orderStateLabel = UILabel()
        self.containerView?.addSubview(self.orderStateLabel!)
        self.orderStateLabel?.snp.makeConstraints { make in
            make.trailing.equalTo(self.containerView!).offset(-12)
            make.centerY.equalTo(self.releaseTypeTitleLabel!)
        }
        self.orderStateLabel?.labelConfig(textAlignment: .right, font: UIFont.systemFont(ofSize: 14, weight: .regular), textColor: color("#333333"), numberOfLines: 0)
        
        self.orderStateImageV = UIImageView()
        self.containerView?.addSubview(self.orderStateImageV!)
        self.orderStateImageV?.snp.makeConstraints { make in
            make.trailing.equalTo(self.orderStateLabel!.snp.leading).offset(-5)
            make.centerY.equalTo(self.orderStateLabel!)
            make.size.equalTo(CGSize(width: 16, height: 16))
        }
        
        self.contentLabel = UILabel()
        self.containerView?.addSubview(self.contentLabel!)
        self.contentLabel?.snp.makeConstraints { make in
            make.leading.equalTo(self.containerView!).offset(12)
            make.trailing.equalTo(self.containerView!).offset(-12)
            make.top.equalTo(self.topLine!.snp.bottom)
            make.height.greaterThanOrEqualTo(79)
        }
        self.contentLabel?.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 16, weight: .regular), textColor: color("#333333"), numberOfLines: 0)
        
        self.middleLine = UIView()
        self.containerView?.addSubview(self.middleLine!)
        self.middleLine?.snp.makeConstraints { make in
            make.leading.equalTo(self.containerView!).offset(12)
            make.trailing.equalTo(self.containerView!).offset(-12)
            make.top.equalTo(self.contentLabel!.snp.bottom)
            make.height.equalTo(1)
//            make.bottom.equalTo(self.containerView!).offset(-81)
        }
        self.middleLine?.backgroundColor = color("#F5F6F8")
        
        self.totalPriceLabel = UILabel()
        self.containerView?.addSubview(self.totalPriceLabel!)
        self.totalPriceLabel?.snp.makeConstraints { make in
            make.leading.equalTo(self.containerView!).offset(12)
            make.height.equalTo(28)
            make.top.equalTo(self.middleLine!.snp.bottom).offset(15)
        }
        self.totalPriceLabel?.text = "合计："
        self.totalPriceLabel?.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 16, weight: .regular), textColor: color("#333333"), numberOfLines: 1)
        
//        self.totalPriceLabel = UILabel()
//        self.containerView?.addSubview(self.totalPriceLabel!)
//        self.totalPriceLabel?.snp.makeConstraints { make in
//            make.leading.equalTo(self.totalTitleLabel!.snp.trailing).offset(1)
//            make.height.equalTo(self.totalTitleLabel!)
//        }
//        self.totalPriceLabel?.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 20, weight: .semibold), textColor: color("#FA6161"), numberOfLines: 1)
//        self.totalPriceLabel?.text = "￥10.00"
//        self.totalPriceLabel?.priceString(UIFont.systemFont(ofSize: 12, weight: .regular), UIFont.systemFont(ofSize: 20, weight: .semibold))
        
        self.payTimeLabel = UILabel()
        self.containerView?.addSubview(self.payTimeLabel!)
        self.payTimeLabel?.snp.makeConstraints { make in
            make.leading.equalTo(self.containerView!).offset(12)
            make.top.equalTo(self.totalPriceLabel!.snp.bottom).offset(5)
            make.height.equalTo(17)
        }
        self.payTimeLabel?.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 12, weight: .regular), textColor: color("#9DA1B2"), numberOfLines: 1)
        self.payTimeLabel?.text = "支付时间 : 2025.01.16 13:20:15"
        
        self.bottomLine = UIView()
        self.containerView?.addSubview(self.bottomLine!)
        self.bottomLine?.snp.makeConstraints { make in
            make.top.equalTo(self.middleLine!.snp.bottom).offset(81)
            make.leading.trailing.equalTo(self.containerView!)
            make.bottom.equalTo(self.containerView!).offset(-51)
        }
        self.bottomLine?.backgroundColor = color("#F5F6F8")
        
        self.bottomFunctionView = UIView()
        self.containerView?.addSubview(self.bottomFunctionView!)
        self.bottomFunctionView?.snp.makeConstraints({ make in
            make.leading.equalTo(self.containerView!)
            make.trailing.equalTo(self.containerView!)
            make.top.equalTo(self.bottomLine!.snp.bottom)
            make.bottom.equalTo(self.containerView!)
        })
    }
    
    func refreshData() {
        
        self.custonFunctionContent(orderType: self.orderType)
        
        self.releaseTypeTitleLabel?.text = self.orderModel?.shopName ?? "龙看板"
        
        switch self.orderModel?.orderSaleType ?? "-1" {
        case "3":
            self.releaseTypeTitleLabel?.text = "尨看板"
            self.contentLabel?.text = "尨看板发布一条信息"
        case "4":
            self.releaseTypeTitleLabel?.text = "尨商圈"
            self.contentLabel?.text = "尨商圈发布一条咨询"
        case "5":
            self.releaseTypeTitleLabel?.text = "用户提现"
            self.contentLabel?.text = "用户进行一次提现"
        default:
            break
        }
        
        let priceStr = String.init(format: "合计：￥%.2f", JSON(self.orderModel?.totalAmount as Any).doubleValue/1000.0)
        
        self.totalPriceLabel?.attributedText = priceStr.attributed(firstStr: "合计：", first: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16, weight: .regular),NSAttributedString.Key.foregroundColor: color("#333333")], secondStr: "￥", second: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12, weight: .regular), NSAttributedString.Key.foregroundColor: color("#FA6161")], thirdStr: String(format: "%.2f", JSON(self.orderModel?.totalAmount as Any).doubleValue/1000.0), third: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 20, weight: .semibold),NSAttributedString.Key.foregroundColor: color("#FA6161")])

//        "支付时间 : 2025.01.16 13:20:15"
        self.payTimeLabel?.text = String(format: "支付时间 : %@", self.orderModel?.payTime ?? "")
        
    }
    
    
    func custonFunctionContent(orderType: JDOrderType) {
        
        if orderType == .waitingPay {
            self.cancelButton = UIButton(type: .custom)
            self.cancelButton?.frame = CGRectMake(0, 0, (screenWidth-24)/2.0, 51)
            self.cancelButton?.setTitle("取消订单", for: .normal)
            self.cancelButton?.setTitleColor(color("#8D9198"), for: .normal)
            self.cancelButton?.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .regular)
            
            self.payButton = UIButton(type: .custom)
            self.payButton?.frame = CGRectMake((screenWidth-24)/2.0, 0, (screenWidth-24)/2.0, 51)
            self.payButton?.setTitle("继续付款", for: .normal)
            self.payButton?.setTitleColor(color("#FA6161"), for: .normal)
            self.payButton?.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .regular)
            
            self.bottomFunctionView?.addSubview(self.cancelButton!)
            self.bottomFunctionView?.addSubview(self.payButton!)
            self.orderStateLabel?.text = "5分钟后关闭"
            self.orderStateImageV?.isHidden = true
        } else if orderType == .alreadyPay {
            // 已付款
//            self.cancelButton = UIButton(type: .custom)
//            self.cancelButton?.frame = CGRectMake(0, 0, (screenWidth-24), 51)
//            self.cancelButton?.setTitle("取消订单", for: .normal)
//            self.cancelButton?.setTitleColor(color("#FA6161"), for: .normal)
//            self.cancelButton?.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .regular)
        
            self.bottomLine?.snp.updateConstraints({ make in
                make.bottom.equalTo(self.containerView!).offset(0)
            })
            self.bottomFunctionView?.isHidden = true
            self.orderStateLabel?.isHidden = true
        } else if self.orderType == .sending {
            // 已接单 - 废
            self.bottomLine?.snp.updateConstraints({ make in
                make.bottom.equalTo(self.containerView!).offset(0)
            })
            self.bottomFunctionView?.isHidden = true
            
        } else if self.orderType == .pickup {
            // 待取货
            self.payButton = UIButton(type: .custom)
            self.payButton?.frame = CGRectMake(0, 0, (screenWidth-24), 51)
            self.payButton?.setTitle("确认收货", for: .normal)
            self.payButton?.setTitleColor(color("#FA6161"), for: .normal)
            self.payButton?.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .regular)
            
            self.bottomFunctionView?.addSubview(self.payButton!)
            self.orderStateLabel?.text = "运输中"
            self.orderStateImageV?.isHidden = false
            self.orderStateImageV?.image = UIImage(named: "inTransit")?.withRenderingMode(.alwaysOriginal)
        } else if self.orderType == .completed {
            // 已完成
            self.bottomLine?.snp.updateConstraints({ make in
                make.bottom.equalTo(self.containerView!).offset(0)
            })
            self.bottomFunctionView?.isHidden = true
            self.orderStateLabel?.text = "已收货"
            self.orderStateImageV?.isHidden = false
            self.orderStateImageV?.image = UIImage(named: "gotStatus")?.withRenderingMode(.alwaysOriginal)
        } else if self.orderType == .refusse {
            // 已拒单
            self.bottomLine?.snp.updateConstraints({ make in
                make.bottom.equalTo(self.containerView!).offset(0)
            })
            self.bottomFunctionView?.isHidden = true
            self.orderStateLabel?.text = "已拒单"
            self.orderStateImageV?.isHidden = false
            self.orderStateLabel?.textColor = color("#9DA1B2")
            self.orderStateImageV?.image = UIImage(named: "refuseStatus")?.withRenderingMode(.alwaysOriginal)
        }
        
        
    }
    

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
